package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.LandListCallBackBean;
import com.love_weinuo.bean.QuckDownPindanCallBackBean;
import com.love_weinuo.databinding.ActivityDownPindanBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.MainActivity;
import com.love_weinuo.ui.adapter.OnItemClickListener;
import com.love_weinuo.ui.adapter.SelectPindanLandAdapter;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;
import com.love_weinuo.util.ToastUtil;
import com.love_weinuo.util.dialog.InCommonDialog;
import com.love_weinuo.util.dialog.PayDialog;
import com.love_weinuo.util.dialog.SelectDrugDialog;
import com.love_weinuo.util.listener.OnBHClickListener;
import com.love_weinuo.util.listener.OnDrugSelectListener;
import com.love_weinuo.util.listener.OnLonglyListener;
import com.love_weinuo.util.listener.OnPindanPointListener;
import com.love_weinuo.util.rx.RxActivityTool;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

import static com.love_weinuo.bean.Constants.REQUEST_CODE_USER_SEARCH;

/**
 * Created by Administrator on 2019/3/28.
 */

public class DownPidanActivity extends BaseActivity {

    ActivityDownPindanBinding binding;
    SelectPindanLandAdapter adapter;
    private List<LandListCallBackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<Integer> drugIds = new ArrayList<>();
    String landArea, landName, drupName, landId;
    String userId = (String) SpUtils.get("module_user_id", "");
    List<String> landIds = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_down_pindan);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        setBack();
        setTitleText("选择要拼的地块");
        setRightText("完成");
        if (strs.size() == 0) {
            binding.recycler.setVisibility(View.GONE);
            binding.ilModuleNothing.setVisibility(View.VISIBLE);
        }
        adapter = new SelectPindanLandAdapter(this, strs);
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, final int position) {
                LogUtil.e("点击去查看土地对应位置");
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        //药品选择点击监听
        adapter.setOnPindanPointListener(new OnPindanPointListener() {
            public void setOnPindanPointListener(final int position) {
                SelectDrugDialog dialog = new SelectDrugDialog(DownPidanActivity.this, new OnDrugSelectListener() {
                    @Override
                    public void setOnDrugSelectListener(int drugId, String drugName) {
                        drugIds.add(drugId);
                        drupName = drugName;
                        strs.get(position).setDrugname(drugName);
                        strs.get(position).setDrugid(drugId);
                        adapter.notifyItemChanged(position);
                    }
                });
                dialog.show();
            }
        });

        //删除点击监听
        adapter.setOnLonglyListener(new OnLonglyListener() {
            @Override
            public void onLonelyListener(final int pos) {
                InCommonDialog dialog = new InCommonDialog(DownPidanActivity.this);
                dialog.setTitleText("确定删除" + strs.get(pos).getName() + "?");
                dialog.show();
                dialog.setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        strs.remove(pos);
                        adapter.notifyDataSetChanged();
                        if (0 == strs.size()) {
                            binding.recycler.setVisibility(View.GONE);
                            binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onCloseClick() {

                    }
                });
            }
        });

        binding.tvModuleAddLand.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rightText:
                if (landIds.size() == 0) {
                    showInCommonDialog("请选择拼单土地");
                    return;
                }

                String landIdStr = "";
                for (String ger : landIds) {
                    landIdStr = landIdStr + "," + ger;
                }

                if (landIdStr.length() == 1) {
                    showInCommonDialog("请选择拼单土地");
                    return;
                }

                String drupNames = "";
                for (int i = 0; i < strs.size(); i++) {
                    if (0 == strs.get(i).getDrugid()) {
                        showInCommonDialog("请选择" + strs.get(i).getName() + "的喷洒药物");
                        return;
                    }
                    drupNames += "," + strs.get(i).getDrugid();
                }

                if (TextUtils.isEmpty(userId)) {
                    ToastUtil.showToast("自动登录到期，请重新登录");
                    startActivity(new Intent(DownPidanActivity.this, LoginActivity.class));
                    finish();
                }

                quckDownTask(landIdStr.substring(1, landIdStr.length()), drupNames.substring(1, drupNames.length()));
                break;
            case R.id.tvModuleAddLand:
                startActivityForResult(new Intent(DownPidanActivity.this, MyLandActivity.class), REQUEST_CODE_USER_SEARCH);
                break;
            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_USER_SEARCH:
                    landArea = data.getStringExtra("module_land_area");
                    landName = data.getStringExtra("module_land_name");
                    landId = data.getStringExtra("module_land_id");
                    strs.add(new LandListCallBackBean.ResultEntity.ResEntity(landArea, 0,
                            0, landName, 0, "", "", "", 0));
                    if (strs.size() != 0) {
                        binding.ilModuleNothing.setVisibility(View.GONE);
                        binding.recycler.setVisibility(View.VISIBLE);
                    }
                    adapter.notifyDataSetChanged();
                    landIds.add(landId);
                    break;
                default:
                    LogUtil.e("requestCode==>" + requestCode);
            }
        } else {
            LogUtil.e("resultCode==>" + resultCode);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 支付完直接进入等待作业流程
     */
    public void showPayDialog() {
        PayDialog dialog = new PayDialog(DownPidanActivity.this);
        dialog.setOnBHClickListener(new OnBHClickListener() {
            @Override
            public void onConfirmClick() {

            }

            @Override
            public void onCloseClick() {
            }
        });
        dialog.show();
    }

    public void quckDownTask(String landid, String drupId) {
        HTTPAPI.getInstance().quckDownTask(userId, landid, drupId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("立即下单成功" + s);
                QuckDownPindanCallBackBean bean = GsonUtil.GsonToBean(s, QuckDownPindanCallBackBean.class);

                createOrder(bean.getResult().getId());
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("立即下单失败" + e.getMessage());
            }
        });
    }

    public void createOrder(String id) {
        HTTPAPI.getInstance().createOrder(userId, id, "2", new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("创建订单成功" + s);
                ToastUtil.showToast("下单成功");
                RxActivityTool.skipActivityAndFinish(DownPidanActivity.this, MainActivity.class);
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("创建订单失败" + e.getMessage());
            }
        });
    }


}
